package com.wf.smartcomm.server.serviceImpl;


import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.House;
import com.wf.smartcomm.server.bean.UnitMachine;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.UnitMachService;
import com.wf.smartcomm.server.util.ConstantUtils;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-21 上午9:48:45 
 * 
 */
@Service
@Transactional
public class UnitMachServiceImpl implements UnitMachService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(UnitMachine machine) {
		machine.setObjectId(IdUtils.uuid());
		machine.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		machine.setUpdateAt(machine.getCreatedAt());
		factory.getCurrentSession().persist(machine);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByVillage(Village village, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from UnitMachine where village = :village");
		query.setParameter("village", village);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from UnitMachine where village = :village");
		query.setParameter("village", village);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}
	
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listAllByVillage(Village village, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from UnitMachine where village =:village");
		query.setParameter("village", village);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from UnitMachine where village = :village");
		query.setParameter("village", village);
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByHouse(House house, Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select * from t_unitMachine where objectId in (select mid from t_machine_relation_house where hid = :hid) or (vid =:vid and type = 2)").addEntity(UnitMachine.class);
		query.setParameter("hid", house.getObjectId());
		query.setParameter("vid", house.getVillage().getObjectId());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public UnitMachine getByHardId(String id) {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine where hardwareId = :id");
		query.setParameter("id", id);
		return (UnitMachine) query.uniqueResult();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public UnitMachine getByUser(User user) {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine where user = :user");
		query.setParameter("user", user);
		return (UnitMachine) query.uniqueResult();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void list(Page page) {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine");
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void queryByKey(Village village, String key, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from UnitMachine where village =:village and placeDes like :key");
		query.setParameter("village", village);
		query.setParameter("key", "%"+key+"%");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from UnitMachine where village =:village and placeDes like :key");
		query.setParameter("village", village);
		query.setParameter("key", "%"+key+"%");
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public UnitMachine getById(String mid) {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine where objectId =:mid");
		query.setParameter("mid", mid);
		return (UnitMachine) query.uniqueResult();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByHid(String hid, Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select * from t_unitMachine where objectId in (select mid from t_machine_relation_house where hid = :hid) or type = 2").addEntity(UnitMachine.class);
		query.setParameter("hid", hid);
		page.setDataList(query.list());
	}

	@Override
	public void update(UnitMachine um) {
		um.setUpdateAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().update(um);
	}

	@Override
	public void report(UnitMachine um) {
		String reportTime = String.valueOf(System.currentTimeMillis());
		Query query = factory.getCurrentSession().createSQLQuery("update t_unitMachine set lastReportTime =:time, online = 1 where objectId =:mid");
		query.setParameter("time", reportTime);
		query.setParameter("mid", um.getObjectId());
		query.executeUpdate();
	}

	@Override
	public void offLine(UnitMachine um) {
		Query query = factory.getCurrentSession().createSQLQuery("update t_unitMachine set online = 0 where objectId =:mid");
		query.setParameter("mid", um.getObjectId());
		query.executeUpdate();
	}

	@Override
	public void delete(UnitMachine um) {
		Query query = factory.getCurrentSession().createSQLQuery("delete from t_machine_relation_house where mid =:id");
		query.setParameter("id", um.getObjectId());
		query.executeUpdate();
		factory.getCurrentSession().delete(um);
	}
	
   public void saveHouseRelation(UnitMachine um, List<House> houses)
   {
     if ((houses != null) && (houses.size() > 0)) {
       String sql = "insert into t_machine_relation_house (mid,hid) values (:mid,:hid)";
       Query query = this.factory.getCurrentSession().createSQLQuery(sql);
       query.setParameter("mid", um.getObjectId());
       for (House house : houses) {
         query.setParameter("hid", house.getObjectId());
         query.executeUpdate();
       }
     }
   }

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void counts(Village loginVillage, Page page) {
		if (loginVillage != null) {
			String hql = "select count(*) from UnitMachine where village =:village";
			Query query = factory.getCurrentSession().createQuery(hql);
			query.setParameter("village", loginVillage);
			Long counts = (Long) query.uniqueResult();
			page.setTotalCount(counts.intValue());
			return;
		}
		
		String hql = "select count(*) from UnitMachine";
		Query query = factory.getCurrentSession().createQuery(hql);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listAll(Page page) {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine");
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<UnitMachine> listCenterMachine() {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine where type =:type");
		query.setParameter("type", ConstantUtils.UM_TYPE_REGION);
		return query.list();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<UnitMachine> listOffLineMachine() {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine where online =:off_line");
		query.setParameter("off_line", "0");
		return query.list();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByAddress(String objectId, String region, String building,
			String entrance, Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select * from t_unitMachine where vid =:vid and region =:region and building =:building and entrance =:entrance").addEntity(UnitMachine.class);
		query.setParameter("vid", objectId);
		query.setParameter("region", region);
		query.setParameter("building", building);
		query.setParameter("entrance", entrance);
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public UnitMachine getByVoip(String voipAccount) {
		String sql = "select * from t_unitMachine where voipNumber = :account";
		Query query = factory.getCurrentSession().createSQLQuery(sql).addEntity(UnitMachine.class);
		query.setParameter("account", voipAccount);
		return (UnitMachine) query.uniqueResult();
	}
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public UnitMachine getByLandVoip(String voipAccount) {
		String sql = "select * from t_unitMachine where voipLandingAccount = :account";
		Query query = factory.getCurrentSession().createSQLQuery(sql).addEntity(UnitMachine.class);
		query.setParameter("account", voipAccount);
		return (UnitMachine) query.uniqueResult();
	}

	@Override
	public List<UnitMachine> listOffLineMachine(Village village) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void listAllUn(Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from UnitMachine where village = null");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		String sql = "select * from t_unitMachine where vid is null";
		query = factory.getCurrentSession().createSQLQuery(sql).addEntity(UnitMachine.class);
		page.setDataList(query.list());
	}

	@Override
	public void delete(String id) {
		String sql = "delete from t_unitMachine where objectId = :id";
		Query query = factory.getCurrentSession().createSQLQuery(sql);
		query.setParameter("id", id);
		query.executeUpdate();
	}

	@Override
	public List<UnitMachine> listByUpdate(Village village, String type, Integer screenSize) {
		String hql = "from UnitMachine where village =:village and type =:type and screenSize =:screenSize";
		Query query = factory.getCurrentSession().createQuery(hql);
		query.setParameter("village", village);
		query.setParameter("type", type);
		query.setParameter("screenSize", screenSize);
		return query.list();
	}

	@Override
	public List<UnitMachine> listAllByVillage(Village village) {
		Query query = factory.getCurrentSession().createQuery("from UnitMachine where village = :village");
		query.setParameter("village", village);
		return query.list();
	}
}
